{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt \n",
    "import pandas as pd\n",
    "from mpl_toolkits.mplot3d import Axes3D\n",
    "from scipy.interpolate import griddata\n",
    "from matplotlib import cm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 让图片中可以显示中文\n",
    "plt.rcParams['font.sans-serif'] = 'SimHei'\n",
    "# 让图片中可以显示负号\n",
    "plt.rcParams['axes.unicode_minus'] = False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "file_path = \"/home/shuai/视频/old20240324.csv\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "文件夹 /home/shuai/视频/old20240324 及其内容已删除\n",
      "文件夹 /home/shuai/视频/old20240324 创建成功\n",
      "文件 /home/shuai/视频/old20240324.zip 已删除\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "import shutil\n",
    "\n",
    "def create_folder_if_not_exists(folder_path):\n",
    "    if not os.path.exists(folder_path):\n",
    "        if os.path.splitext(folder_path)[1].lower() != '.zip':\n",
    "            os.makedirs(folder_path)\n",
    "            print(f\"文件夹 {folder_path} 创建成功\")\n",
    "    else:\n",
    "        if os.path.isfile(folder_path):\n",
    "            os.remove(folder_path)\n",
    "            print(f\"文件 {folder_path} 已删除\")\n",
    "        elif os.path.isdir(folder_path):\n",
    "            shutil.rmtree(folder_path)\n",
    "            print(f\"文件夹 {folder_path} 及其内容已删除\")\n",
    "            os.makedirs(folder_path)\n",
    "            print(f\"文件夹 {folder_path} 创建成功\")\n",
    "\n",
    "# 提取文件名称和路径\n",
    "file_name = os.path.splitext(os.path.basename(file_path))[0]\n",
    "directory_path = os.path.dirname(file_path)\n",
    "# 你可以调用这个函数，并传入需要检查的文件夹路径\n",
    "folder_path = directory_path + \"/\" + file_name\n",
    "zip_path = directory_path + \"/\" + file_name + \".zip\"\n",
    "create_folder_if_not_exists(folder_path)\n",
    "create_folder_if_not_exists(zip_path)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>P0</th>\n",
       "      <th>P1</th>\n",
       "      <th>P2</th>\n",
       "      <th>P3</th>\n",
       "      <th>P4</th>\n",
       "      <th>P5</th>\n",
       "      <th>P6</th>\n",
       "      <th>P7</th>\n",
       "      <th>P8</th>\n",
       "      <th>P9</th>\n",
       "      <th>...</th>\n",
       "      <th>P26</th>\n",
       "      <th>P27</th>\n",
       "      <th>P28</th>\n",
       "      <th>P29</th>\n",
       "      <th>P30</th>\n",
       "      <th>P31</th>\n",
       "      <th>P32</th>\n",
       "      <th>P33</th>\n",
       "      <th>P34</th>\n",
       "      <th>P35</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1.5</td>\n",
       "      <td>0.5144</td>\n",
       "      <td>13768.68066</td>\n",
       "      <td>286696.8125</td>\n",
       "      <td>28747.72070</td>\n",
       "      <td>354003.7188</td>\n",
       "      <td>34314.93359</td>\n",
       "      <td>357846.5000</td>\n",
       "      <td>56136.91797</td>\n",
       "      <td>395813.1875</td>\n",
       "      <td>...</td>\n",
       "      <td>96071.80469</td>\n",
       "      <td>368650.3438</td>\n",
       "      <td>59487.93359</td>\n",
       "      <td>296156.0000</td>\n",
       "      <td>8303.420898</td>\n",
       "      <td>159177.4844</td>\n",
       "      <td>71481.86719</td>\n",
       "      <td>3.669561e+05</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1.5</td>\n",
       "      <td>1.0290</td>\n",
       "      <td>15661.30762</td>\n",
       "      <td>239330.3906</td>\n",
       "      <td>30706.92188</td>\n",
       "      <td>278924.6875</td>\n",
       "      <td>35697.22656</td>\n",
       "      <td>279872.7500</td>\n",
       "      <td>56019.22266</td>\n",
       "      <td>327893.5000</td>\n",
       "      <td>...</td>\n",
       "      <td>96441.28125</td>\n",
       "      <td>363170.9375</td>\n",
       "      <td>58987.83203</td>\n",
       "      <td>288542.5625</td>\n",
       "      <td>5880.359375</td>\n",
       "      <td>130922.3516</td>\n",
       "      <td>62098.50391</td>\n",
       "      <td>3.350240e+05</td>\n",
       "      <td>0.221023</td>\n",
       "      <td>0.334890</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1.5</td>\n",
       "      <td>1.5430</td>\n",
       "      <td>14526.57227</td>\n",
       "      <td>227596.4688</td>\n",
       "      <td>27759.19727</td>\n",
       "      <td>266642.8125</td>\n",
       "      <td>32271.85352</td>\n",
       "      <td>275117.9375</td>\n",
       "      <td>51267.17188</td>\n",
       "      <td>325726.9375</td>\n",
       "      <td>...</td>\n",
       "      <td>97583.65625</td>\n",
       "      <td>351476.6875</td>\n",
       "      <td>58434.10156</td>\n",
       "      <td>289191.4063</td>\n",
       "      <td>5149.017090</td>\n",
       "      <td>136740.6406</td>\n",
       "      <td>59760.77734</td>\n",
       "      <td>3.146491e+05</td>\n",
       "      <td>0.214608</td>\n",
       "      <td>0.315642</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2.0</td>\n",
       "      <td>0.5144</td>\n",
       "      <td>36442.47656</td>\n",
       "      <td>552685.3750</td>\n",
       "      <td>57913.85547</td>\n",
       "      <td>754881.6875</td>\n",
       "      <td>64295.28906</td>\n",
       "      <td>780469.2500</td>\n",
       "      <td>93842.42969</td>\n",
       "      <td>892707.5000</td>\n",
       "      <td>...</td>\n",
       "      <td>129560.60940</td>\n",
       "      <td>842952.8750</td>\n",
       "      <td>87147.53125</td>\n",
       "      <td>708814.8750</td>\n",
       "      <td>50588.695310</td>\n",
       "      <td>621670.2500</td>\n",
       "      <td>153912.14060</td>\n",
       "      <td>1.021330e+06</td>\n",
       "      <td>1.127920</td>\n",
       "      <td>1.104291</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2.0</td>\n",
       "      <td>1.0290</td>\n",
       "      <td>35045.92188</td>\n",
       "      <td>498700.3438</td>\n",
       "      <td>58710.47656</td>\n",
       "      <td>656219.5625</td>\n",
       "      <td>64407.51953</td>\n",
       "      <td>671388.8750</td>\n",
       "      <td>90042.95313</td>\n",
       "      <td>754463.6875</td>\n",
       "      <td>...</td>\n",
       "      <td>129916.44530</td>\n",
       "      <td>662650.7500</td>\n",
       "      <td>87981.36719</td>\n",
       "      <td>588829.7500</td>\n",
       "      <td>40036.625000</td>\n",
       "      <td>477864.0313</td>\n",
       "      <td>101340.08590</td>\n",
       "      <td>7.536669e+05</td>\n",
       "      <td>0.538425</td>\n",
       "      <td>0.781815</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 36 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "    P0      P1           P2           P3           P4           P5  \\\n",
       "0  1.5  0.5144  13768.68066  286696.8125  28747.72070  354003.7188   \n",
       "1  1.5  1.0290  15661.30762  239330.3906  30706.92188  278924.6875   \n",
       "2  1.5  1.5430  14526.57227  227596.4688  27759.19727  266642.8125   \n",
       "3  2.0  0.5144  36442.47656  552685.3750  57913.85547  754881.6875   \n",
       "4  2.0  1.0290  35045.92188  498700.3438  58710.47656  656219.5625   \n",
       "\n",
       "            P6           P7           P8           P9  ...           P26  \\\n",
       "0  34314.93359  357846.5000  56136.91797  395813.1875  ...   96071.80469   \n",
       "1  35697.22656  279872.7500  56019.22266  327893.5000  ...   96441.28125   \n",
       "2  32271.85352  275117.9375  51267.17188  325726.9375  ...   97583.65625   \n",
       "3  64295.28906  780469.2500  93842.42969  892707.5000  ...  129560.60940   \n",
       "4  64407.51953  671388.8750  90042.95313  754463.6875  ...  129916.44530   \n",
       "\n",
       "           P27          P28          P29           P30          P31  \\\n",
       "0  368650.3438  59487.93359  296156.0000   8303.420898  159177.4844   \n",
       "1  363170.9375  58987.83203  288542.5625   5880.359375  130922.3516   \n",
       "2  351476.6875  58434.10156  289191.4063   5149.017090  136740.6406   \n",
       "3  842952.8750  87147.53125  708814.8750  50588.695310  621670.2500   \n",
       "4  662650.7500  87981.36719  588829.7500  40036.625000  477864.0313   \n",
       "\n",
       "            P32           P33       P34       P35  \n",
       "0   71481.86719  3.669561e+05  0.000000  0.000000  \n",
       "1   62098.50391  3.350240e+05  0.221023  0.334890  \n",
       "2   59760.77734  3.146491e+05  0.214608  0.315642  \n",
       "3  153912.14060  1.021330e+06  1.127920  1.104291  \n",
       "4  101340.08590  7.536669e+05  0.538425  0.781815  \n",
       "\n",
       "[5 rows x 36 columns]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "(45, 36)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 读取Excel文件，忽略前6行\n",
    "df0 = pd.read_csv(file_path)\n",
    "display(df0.head())\n",
    "display(df0.shape)\n",
    "df0_rows = int(df0.shape[0]/9)\n",
    "df0_cols = int(df0.shape[1]-2)\n",
    "df0_values = df0.values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 定义插值函数\n",
    "def interpolate_surface(x, y, z):\n",
    "    # 创建网格点\n",
    "    xi = np.linspace(x.min(), x.max(), 100)\n",
    "    yi = np.linspace(y.min(), y.max(), 100)\n",
    "    xi, yi = np.meshgrid(xi, yi)\n",
    "    \n",
    "    # 进行插值\n",
    "    zi = griddata((x, y), z, (xi, yi), method='cubic')\n",
    "    return xi, yi, zi"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 循环处理每个子图\n",
    "for j in range(df0_cols):\n",
    "    for i in range(df0_rows):\n",
    "        x = df0_values[i*9:(i+1)*9, 1].reshape(-1)\n",
    "        y = df0_values[i*9:(i+1)*9, 0].reshape(-1)\n",
    "        if j < 32: \n",
    "            z = df0_values[i*9:(i+1)*9, j+2].reshape(-1) / 10000\n",
    "        else:\n",
    "            z = df0_values[i*9:(i+1)*9, j+2].reshape(-1)\n",
    "        \n",
    "        # 插值得到曲面\n",
    "        xi, yi, zi = interpolate_surface(x, y, z)\n",
    "        \n",
    "        # 创建一个三维坐标系\n",
    "        fig = plt.figure(figsize=(140/25.4,140/25.4))\n",
    "        ax = fig.add_subplot(111, projection='3d')\n",
    "        # 绘制曲面\n",
    "        ax.plot_surface(xi, yi, zi, cmap=cm.coolwarm)\n",
    "        if np.any(zi >=49): \n",
    "            ax.contourf(xi, yi, zi, levels=[49.9,50.1], colors='black')\n",
    "        if np.any(zi >=195): \n",
    "            ax.contourf(xi, yi, zi, levels=[195.9,196.1], colors='green')\n",
    "        # 绘制散点图\n",
    "        ax.scatter(x, y, z)\n",
    "\n",
    "        file_save_path = \"\"\n",
    "        if j < 32: \n",
    "            jun_zui = '均值' if j%2==0 else '最值'\n",
    "            ax.set_xlabel('流速(m/s)', fontsize=9)\n",
    "            ax.set_ylabel('波高(m)', fontsize=9)\n",
    "            ax.set_zlabel('缆绳张力(t)', fontsize=9)\n",
    "            plt.title(f'{i*45}度缆绳{int(j/2)+1}张力{jun_zui}')\n",
    "            file_save_path = f'{folder_path}/{i*45}度缆绳{int(j/2)+1}张力{jun_zui}.jpg'\n",
    "        else:\n",
    "            heng_zong = '横荡' if j%2==0 else '纵荡'\n",
    "            ax.set_xlabel('流速(m/s)', fontsize=9)\n",
    "            ax.set_ylabel('波高(m)', fontsize=9)\n",
    "            ax.set_zlabel(f'{heng_zong}(m)', fontsize=9)\n",
    "            plt.title(f'{i*45}度船舶{heng_zong}运动幅值')\n",
    "            file_save_path = f'{folder_path}/{i*45}度船舶{heng_zong}运动幅值.jpg'\n",
    "        # 设置边缘空白大小    \n",
    "        plt.subplots_adjust(left=0.05, right=0.85, top=0.9, bottom=0)\n",
    "        plt.savefig(file_save_path)\n",
    "        # 关闭图形，以便下一次循环创建新的图形\n",
    "        plt.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "import zipfile\n",
    "\n",
    "def zip_folder(folder_path, zip_file_name):\n",
    "    with zipfile.ZipFile(zip_file_name, 'w', zipfile.ZIP_DEFLATED) as zipf:\n",
    "        for root, dirs, files in os.walk(folder_path):\n",
    "            for file in files:\n",
    "                file_path = os.path.join(root, file)\n",
    "                zipf.write(file_path, os.path.relpath(file_path, folder_path))\n",
    "\n",
    "# 定义要压缩的文件夹路径和ZIP文件名\n",
    "zip_file_name = folder_path + \".zip\"\n",
    "\n",
    "# 调用函数压缩文件夹\n",
    "zip_folder(folder_path, zip_file_name)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "yolo_learn",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.18"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
